<?php
defined('BASEPATH') or exit('No direct script access allowed');

use Rhilip\Ipv6Wry\IpLocation;
use think\api\Client;

class Welcome extends MY_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct()
    {
        parent::__construct();
        header("Access-Control-Allow-Origin: * ");
        $this->load->model(['site_model', 'sitecategory_model', 'siteproduct_model', 'siteuser_model', 'siteorder_model', 'sf_model']);
        $this->data['page_home'] = lang('lang_dashboard');
        $this->data['page_title'] = lang('lang_order');

        $this->data['statetype'] = [
            "1" => '未付款',
            "2" => '已付款',
            "3" => '采购中',
            "4" => '已发货',
            "5" => '已完成',
            "6" => '取消',
            "7" => '退货',
            "8" => '付款出错',
            "9" => 'ECHECK',
            "10" => 'BACKORDER'
        ];
        $this->data['statelabel'] = [
            "1" => 'label-danger',
            "2" => 'label-success',
            "3" => 'label-primary',
            "4" => 'label-info',
            "5" => 'label-info',
            "6" => 'label-warning',
            "7" => 'label-warning',
            "8" => 'label-danger',
            "9" => 'label-warning',
        ];
    }

    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function ordersmobileget()
    {
        // Title
        $this->data['meta_title'] = $this->data['page_title'];

        // Breadcrumbs
        $this->data['array_breadcrumbs'] = array(
            $this->data['page_home'] => 'backend',
            '所有询盘' => 'backend/orders'
        );


        $key = trim($this->input->get('keyword'));
        if (substr($key, -1) == ">") {
            $key = substr($key, 0, -1);
        }

        $page = max(1, (int)$this->input->get('page'));
        $sitetype = intval($this->input->get('sitetype'));
        $shopfrom = trim($this->input->get('shopfromm'));
        $pagesize = intval($this->input->get('pagesize'));
        $timelimit = $this->input->get("timelimit");

        if ($pagesize) {
            set_cookie("pagesize", $pagesize, 60 * 60 * 24);
        } else {
            $pagesize = get_cookie("pagesize");
        }
        $this->data['sindex'] = $this->site_model->get_name_index();

        $siteid = [];
        if ($key) {
            foreach ($this->data['sindex'] as $k => $v) {
                if (strstr(strtolower($v), strtolower($key))) {
                    $siteid[] = $k;
                }
            }
            if (count($siteid) > 0) {
                $key = "";
            }
        }
        if ($sitetype > 0) {
            $sites = $this->site_model->getsitetype($sitetype);
            if ($sites) {
                $siteid = [];
                foreach ($sites as $s) {
                    $siteid[] = $s["id"];
                }
            }
        }
        $spsite = [];
        $spsites = [];
        $shopifysite = $this->site_model->getallsync();
        if ($shopifysite):
            foreach ($shopifysite as $ac) {
                $spsite[] = $ac["id"];
                $spsites[$ac["id"]] = $ac;
            }
        endif;
        $this->data['spsite'] = $spsite;
        $this->data['spsites'] = $spsites;

        $state = 0;
        $siteid = (int)$this->input->get('state');
        $except = (int)$this->input->get('except');

        $export = intval($this->input->get('export'));
        if ($export == 1) {
            $orders = $this->siteorder_model->getbyfilter($siteid, $key, 0, $state, $timelimit, $except, $shopfrom);
            foreach ($orders as $k => $v) {
                $v = $this->site_model->get_name_by_id($orders[$k]['siteid']);
                $orders[$k]['siteid'] = $v['name'];
            }
            $this->makexlsx($orders);
        }
        $page_data = $this->siteorder_model->getpage($siteid, $page, $pagesize = 50, $key, 0, $state, $timelimit, $except, $shopfrom);
        $this->data['loglist'] = $page_data["data"];
        $this->data['pager'] = $page_data["pager"];
        $this->data['total'] = $page_data["total"];
        $this->data['pages'] = $page_data["pages"];
        $this->data['page'] = $page;
        $this->data['sitetype'] = $this->config->item('site_types');
        $this->data['state'] = $state;
        $this->data['shopfrom'] = $shopfrom;
        $this->data['cdmap'] = $this->sf_model->getcdmap();


        $keyword = array(
            'name' => 'keyword',
            'id' => 'keyword',
            'value' => $key,
            'class' => 'form-control input-sm',
            'placeholder' => '快速查询关键字',
            'maxlength' => '100'
        );
        $this->data['keyword'] = $keyword;
        $this->data['content'] = 'orders2';
        $data = $page_data["data"];

        foreach ($data as &$k) {
            if (ismobile($k["agent"]) == 1) {
                $k["agent"] = 'mobile';
            } else {
                $k["agent"] = 'pc';
            }
            $siteName = $this->site_model->getitembyid($k["siteid"]);
            $k["siteid"] = $siteName["name"];
        }

        echo json_encode($data);
    }

    public function get_info()
    {
        $result = api()->taobaoDetail()
            ->withId('')
            ->request();
        print_r($result);
    }

    public function get_ip()
    {
        $ip = '2a00:23c5:c2af:5401:d576:db4:6768:cd83';
        $ip2region = IpLocation::searchIp($ip);
        $country = [
                'country'=>explode(" ", $ip2region['area'])[0]
        ];
        echo $country["country"];
    }
}
